package com.lxy.controller;

import com.lxy.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 请不要模仿我
 *
 * @author lxy
 * @date 2022/9/28 14:52
 * com.lxy.login.controller.
 */
@Controller
@RequestMapping("/user")
public class LoginController {
    /*
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    * */
    @RequestMapping("/login")
    public String login(String uname,String pwd){
        System.out.println(uname+"     "+pwd);
        return "hello";
    }
    @RequestMapping("/test")
    public String test(String name, @DateTimeFormat(pattern="yyyy-MM-dd")Date date1){
        System.out.println(name);
        System.out.println("日期格式"+date1);
        return "hello";
    }
    @RequestMapping("/test1")
    public String test1(String name, @DateTimeFormat(pattern="yyyy-MM-dd T HH:mm:ss") Date date1){
        System.out.println(name);
        System.out.println("日期格式"+date1);
        return "hello";
    }
    @RequestMapping("/test2")
    public String test2(){

        return "forward:/hello.jsp";
    }
    @RequestMapping("/json")//user/json
    @ResponseBody //将handler的返回值，转换成json(jackson),并将json响应给客户端。
    public User hello1(){
        System.out.println("hello world");
        User user = new User();
        user.setAddress("张三");
        return user;
    }
    /*Controller类上加了@RestController注解，等价于在类中的每个方法上都加了@ResponseBody
    */
//    @RequestMapping("/rest")
//    @RestController
//    public
    @RequestMapping("/users")
    public String addUser(@RequestBody User user){
        //@RequestBody将请求体中的json数据转换为java对象
        System.out.println("cap2");
        System.out.println("Post user :"+user);
        return "redirect:/index.jsp";
//        redirect:/view/user.jsp
    }

}
